package ReflectTest;

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

/*
通过反射机制获取类的构造方法,反编译构造方法
 */
public class ReflectConstructorTest1 {
    public static void main(String[] args) {
        try {
            StringBuilder builder = new StringBuilder();
            Class c = Class.forName("java.lang.String");
            Object o = c.newInstance();
            Constructor[] constructors = c.getConstructors();
            for(Constructor constructor : constructors){
                builder.append("\t");
                builder.append(Modifier.toString(constructor.getModifiers()));
                builder.append(" ");
                builder.append(c.getSimpleName());
                builder.append("(");
                Class[] classes = constructor.getParameterTypes();
                for(Class c1 : classes){
                    builder.append(c1.getSimpleName());
                    builder.append(",");
                }
                if(builder.charAt(builder.length() - 1) == ','){
                    builder.deleteCharAt(builder.length() - 1);
                }
                builder.append("){}\n");
            }
            System.out.println(builder);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
